package com.fourGirlsAndADude.mobileDRT;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class ReportSelectionActivity extends Activity {
	private String userName;
	private String authType;
	private String token;

	private Spinner year;
	private Spinner type;
	private Spinner name;

	private ArrayAdapter<String> yearAdapter;
	private ArrayAdapter<String> typeAdapter;
	private ArrayAdapter<String> nameAdapter;

	boolean yearSelected = false;
	boolean typeSelected = false;
	boolean nameSelected = false;

	private int yr;
	private String title;

	private Button linkAdmin;
	private Button linkCRE;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_report_selection);

		// Receiving extras from previous activity
		Intent intent = getIntent();
		userName = intent.getStringExtra("USERNAME");
		authType = intent.getStringExtra("AUTHORIZATION_TYPE");
		token = intent.getStringExtra("SESSION_TOKEN");

		// Create Spinner for year and respond to input
		year = (Spinner) findViewById(R.id.yearSpinner);
		yearAdapter = new ArrayAdapter<String>(ReportSelectionActivity.this,
				android.R.layout.simple_list_item_1, getList(R.array.eventYear));
		yearAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		year.setAdapter(yearAdapter);
		year.setOnItemSelectedListener(new yearOnSelectedItemClickListener());

		// Create Spinner for type and respond to input
		type = (Spinner) findViewById(R.id.typeSpinner);
		typeAdapter = new ArrayAdapter<String>(ReportSelectionActivity.this,
				android.R.layout.simple_spinner_dropdown_item,
				getList(R.array.plzSelectYear));
		typeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		type.setAdapter(typeAdapter);
		type.setOnItemSelectedListener(new typeOnSelectedItemClickListener());

		// Create Spinner for name and respond to input
		name = (Spinner) findViewById(R.id.nameSpinner);
		nameAdapter = new ArrayAdapter<String>(ReportSelectionActivity.this,
				android.R.layout.simple_spinner_dropdown_item,
				getList(R.array.plzSelectYear));
		nameAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		name.setAdapter(nameAdapter);
		name.setOnItemSelectedListener(new nameOnSelectedItemClickListener());

		linkAdmin = (Button) findViewById(R.id.adminButton);
		if (authType.equals("RPT")) {
			linkAdmin.setVisibility(Button.GONE);
			linkAdmin.setClickable(false);
		}
		linkAdmin.setOnClickListener(new LinkAdminOnClickListener());

		linkCRE = (Button) findViewById(R.id.creButton);
		linkCRE.setOnClickListener(new LinkCreOnClickListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.report_selection, menu);
		return true;
	}

	private class yearOnSelectedItemClickListener implements
			android.widget.AdapterView.OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			int year;
			try {
				year = Integer.parseInt((String) arg0.getItemAtPosition(arg2));
			} catch (NumberFormatException e) {
				year = 0;
			}
			switch (year) {
			case 2010:
				typeAdapter = new ArrayAdapter<String>(getBaseContext(),
						android.R.layout.simple_list_item_1,
						getList(R.array.event_type_2010));
				yearSelected = true;
				break;
			case 2011:
				typeAdapter = new ArrayAdapter<String>(getBaseContext(),
						android.R.layout.simple_list_item_1,
						getList(R.array.event_type_2011));
				yearSelected = true;
				break;
			case 2012:
				typeAdapter = new ArrayAdapter<String>(getBaseContext(),
						android.R.layout.simple_list_item_1,
						getList(R.array.event_type_2012));
				yearSelected = true;
				break;
			case 2013:
				typeAdapter = new ArrayAdapter<String>(getBaseContext(),
						android.R.layout.simple_list_item_1,
						getList(R.array.event_type_2013));
				yearSelected = true;
				break;
			default:
				yearSelected = false;
				break;
			}
			yr = year;
			typeAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			type.setAdapter(typeAdapter);
			if (yr != 0) {
				yearSelected = true;
			} else {
				yearSelected = false;
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}
	}

	private class typeOnSelectedItemClickListener implements
			android.widget.AdapterView.OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Log.v("On type selected", "yr: " + yr);
			switch (arg2) {
			case 0:
				nameAdapter = new ArrayAdapter<String>(getBaseContext(),
						android.R.layout.simple_list_item_1,
						getList(R.array.plzSelectType));
				break;
			case 1:

				switch (yr) {
				case 2013:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.hurricane_2013));
					break;
				case 2012:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.hurricane_12));
					break;
				case 2011:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.hurricane_11));
					break;
				case 2010:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.hurricane_10));
					break;
				default:

					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.plzSelectType));
					break;
				}
				break;
			case 2:
				switch (yr) {
				case 2013:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.earthquake_2013));
					break;
				case 2012:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.earthquake_12));
					break;
				case 2011:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.earthquake_11));
					break;
				case 2010:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.earthquake_10));
					break;
				default:

					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.plzSelectType));

					break;
				}
				break;
			case 3:
				switch (yr) {
				case 2013:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.flood_13));
					break;
				case 2012:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.flood_12));
					break;
				case 2011:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.flood_11));
					break;
				case 2010:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.flood_10));
					break;
				default:

					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.plzSelectType));

					break;
				}
				break;
			case 4:
				switch (yr) {
				case 2013:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.icestorm_2013));
					break;
				case 2012:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.icestorm_12));
					break;
				case 2011:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.icestorm_11));
					break;
				case 2010:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.icestorm_10));
					break;
				default:

					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.plzSelectType));

					break;
				}
				break;
			case 5:
				switch (yr) {
				case 2013:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.snowstorm_2013));
					break;
				case 2012:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.snowstorm_12));
					break;
				case 2011:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.snowstorm_11));
					break;
				case 2010:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.snowstorm_10));
					break;
				default:

					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.plzSelectType));

					break;
				}
				break;
			case 6:
				switch (yr) {
				case 2013:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.tornado_2013));
					break;
				case 2012:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.tornado_12));
					break;
				case 2011:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.tornado_11));
					break;
				case 2010:
					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.tornado_10));
					break;
				default:

					nameAdapter = new ArrayAdapter<String>(getBaseContext(),
							android.R.layout.simple_list_item_1,
							getList(R.array.plzSelectType));

					break;
				}
				break;
			case 7:
				nameAdapter = new ArrayAdapter<String>(getBaseContext(),
						android.R.layout.simple_list_item_1,
						getList(R.array.hurricane_2013));
				break;
			default:
				nameAdapter = new ArrayAdapter<String>(getBaseContext(),
						android.R.layout.simple_list_item_1,
						getList(R.array.plzSelectType));
				break;
			}

			nameAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			name.setAdapter(nameAdapter);
			if (arg2 != 0) {
				typeSelected = true;
			} else {
				typeSelected = false;
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	}

	private class nameOnSelectedItemClickListener implements
			android.widget.AdapterView.OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			title = arg0.getItemAtPosition(arg2).toString();
			nameSelected = true;
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	}

	private ArrayList<String> getList(int arrayResouce) {
		ArrayList<String> lists = new ArrayList<String>();
		String[] strings = getResources().getStringArray(arrayResouce);
		for (String s : strings) {
			lists.add(s);
		}

		return lists;
	}

	private class LinkAdminOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (yearSelected && nameSelected && typeSelected) {
				Intent intent = new Intent(getBaseContext(),
						AdminConditionCannedReportActivity.class);
				intent.putExtra("EVENT_NAME", title);
				intent.putExtra("EVENT_YEAR", yr);
				intent.putExtra("SESSION_TOKEN", token);
				startActivity(intent);
			} else {
				showMessage();
			}
		}
	}

	private class LinkCreOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (yearSelected && nameSelected && typeSelected) {
				Intent intent = new Intent(getBaseContext(),
						CreClosureOrDelayedOpenActivity.class);
				intent.putExtra("SESSION_TOKEN", token);
				intent.putExtra("EVENT_NAME", title);
				intent.putExtra("EVENT_YEAR", yr);
				startActivity(intent);
			} else {
				showMessage();
			}
		}
	}

	private void showMessage() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(
				ReportSelectionActivity.this);
		dialog.setTitle("What have you done?")
				.setMessage(
						"O. M. G. Not only will the app crash."
								+ " Your entire phone will be wiped clean. "
								+ "In T minus 5, 4, 3, 2, 1. Nah, just kiddin.Please"
								+ " select an option from each drop down, and try again.")
				.setNegativeButton("Exit",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								dialog.cancel();
							}
						});
		AlertDialog showMessage = dialog.create();
		showMessage.show();

	}

}
